package com.demo.d514.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.demo.d514.dao.mapper.UserMapper;
import com.demo.d514.dao.model.User;
import com.demo.d514.dao.model.UserExample;
import com.demo.d514.service.UserService;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private TransactionTemplate transactionTemplate;

	@Override
	public List<User> queryAll() {
		UserExample example = new UserExample();
		return userMapper.selectByExample(example);
	}

	@Override
	public int insert(User user) {
		return userMapper.insertSelective(user);
	}

	@Override
	public int updateById(final User user) {

		int r = userMapper.updateByPrimaryKey(user);
//		if (user.getId() > 0) {
//			throw new RuntimeException();
//		}
		return r;

	}

	// @Override
	// public int updateById(final User user) {
	// int ret = 0;
	// ret = transactionTemplate.execute(new TransactionCallback<Integer>() {
	//
	// @Override
	// public Integer doInTransaction(TransactionStatus status) {
	// int r = 0;
	// try {
	// r = userMapper.updateByPrimaryKey(user);
	// if (user.getId() > 0) {
	// throw new RuntimeException();
	// }
	// } catch (Exception e) {
	// status.setRollbackOnly();
	// throw e;
	// }
	// return r;
	// }
	//
	// });
	//
	// return ret;
	// }

}
